import sys
import time

print("========虚拟减肥跑步机========")
print(30 * "#")
while True:    
    reduce = input("输入您要减肥的体重（kg）：")
    if not reduce.replace(".", '').isdigit():
        print('输入非数字字符，请重新输入！') 
        continue
    if float(reduce) < 2 or float(reduce) > 50:
        print('输入减肥重量必须在 2~50kg 范围内，请重新输入！')
        continue
    reduce = float(reduce)
    break

while True:
    age = input("输入您的年龄：")
    if not age.isdigit():
        print('输入非法字符，请重新输入！')
        continue
    if int(age) < 8 or int(age) > 100:
        print('输入年龄必须在 8~100 岁范围内，请重新输入！')
        continue
    age = int(age)
    break
day = reduce / 0.15
heart = (220 - age) * 0.65
print('减肥{}公斤，需要{:.0f}天最佳运动'.format(reduce, day))
print('年龄{}岁，适合跑步的心率为{:.0f}'.format(age, heart))
weight = float(input("输入您的体重（KG）："))
speed = float(input("速度（公里/小时）："))
times = int(input("跑步时间（分钟）："))
times *= 60
leave = 0
while leave < times:
    min, sec = divmod(times - leave, 60)
    leave_time = str(min) + '分' + str(sec) + '秒'
    dista = leave / 3600 * speed
    calor = weight * 30 / (400 / (speed * 1000 / 60)) * leave / 60 / 60
    sys.stdout.write('\r')  
    sys.stdout.write('剩余时间：{} 跑步距离:{:.2f} 公里 消耗热量：{:.2f} 千卡'.format(leave_time, dista, calor))
    time.sleep(1)
    leave += 1
